﻿using System;
using System.Net;
using SomeKit.Http;
using SomeKit.Serialization;

namespace SomeKit.TypeSafe
{
    public static partial class ClientHelper
    {
        /// <summary>
        /// Executes the "PUT" verb against the resource located at <paramref name="uri"/>.
        /// </summary>
        /// <param name="uri">The resource to be accessed.</param>
        /// <param name="errorHandler">The error handler.</param>
        /// <param name="headerHandler">Custom header handler.</param>
        public static void Put(this Uri uri, Action<Exception> errorHandler, Action<HttpWebRequest> headerHandler = null)
        {
            uri.Put(EnsureEmptyResultHandler(errorHandler), errorHandler, headerHandler);
        }

        /// <summary>
        /// Executes the "PUT" verb against the resource located at <paramref name="uri"/>.
        /// </summary>
        /// <param name="uri">The resource to be accessed.</param>
        /// <param name="handler">The final handler.</param>
        /// <param name="errorHandler">The error handler.</param>
        /// <param name="headerHandler">Custom header handler.</param>
        public static void Put(this Uri uri, Action handler, Action<Exception> errorHandler, Action<HttpWebRequest> headerHandler = null)
        {
            uri.Put(EnsureEmptyResultHandler(errorHandler, handler), errorHandler, headerHandler);
        }

        /// <summary>
        /// Executes the "PUT" verb against the resource located at <paramref name="uri"/>.
        /// </summary>
        /// <typeparam name="BODY">A type of object to be sent to the server.</typeparam>
        /// <param name="uri">The resource to be accessed.</param>
        /// <param name="body">The body of the request.</param>
        /// <typeparam name="BODY">A type of object to be sent to the server.</typeparam>
        /// <param name="errorHandler">The error handler.</param>
        /// <param name="serializer">A method that is capable of serializing <paramref name="body"/>.</param>
        /// <param name="headerHandler">Custom header handler.</param>
        public static void Put<BODY>(this Uri uri, BODY body, Action<Exception> errorHandler, Func<BODY, string> serializer, Action<HttpWebRequest> headerHandler = null)
        {
            uri.Put(serializer(body), EnsureEmptyResultHandler(errorHandler), errorHandler, headerHandler);
        }
        /// <summary>
        /// Executes the "PUT" verb against the resource located at <paramref name="uri"/>. The request body is expected to be formatted in XML.
        /// </summary>
        /// <typeparam name="BODY">A type of object to be sent to the server.</typeparam>
        /// <param name="uri">The resource to be accessed.</param>
        /// <param name="body">The body of the request.</param>
        /// <param name="errorHandler">The error handler.</param>
        /// <param name="headerHandler">Custom header handler.</param>
        public static void PutXml<BODY>(this Uri uri, BODY body, Action<Exception> errorHandler, Action<HttpWebRequest> headerHandler = null)
        {
            uri.PutXml(body, () => { }, errorHandler, headerHandler);
        }
        /// <summary>
        /// Executes the "PUT" verb against the resource located at <paramref name="uri"/>. The request body is expected to be formatted in JSON.
        /// </summary>
        /// <typeparam name="BODY">A type of object to be sent to the server.</typeparam>
        /// <param name="uri">The resource to be accessed.</param>
        /// <param name="body">The body of the request.</param>
        /// <param name="errorHandler">The error handler.</param>
        /// <param name="headerHandler">Custom header handler.</param>
        public static void PutJson<BODY>(this Uri uri, BODY body, Action<Exception> errorHandler, Action<HttpWebRequest> headerHandler = null)
        {
            uri.PutJson(body, () => { }, errorHandler, headerHandler);
        }

        /// <summary>
        /// Executes the "PUT" verb against the resource located at <paramref name="uri"/>.
        /// </summary>
        /// <typeparam name="BODY">A type of object to be sent to the server.</typeparam>
        /// <param name="uri">The resource to be accessed.</param>
        /// <param name="body">The body of the request.</param>
        /// <typeparam name="BODY">A type of object to be sent to the server.</typeparam>
        /// <param name="errorHandler">The error handler.</param>
        /// <param name="serializer">A method that is capable of serializing <paramref name="body"/>.</param>
        /// <param name="headerHandler">Custom header handler.</param>
        public static void Put<BODY>(this Uri uri, BODY body, Action handler, Action<Exception> errorHandler, Func<BODY, string> serializer, Action<HttpWebRequest> headerHandler = null)
        {
            uri.Put(serializer(body), EnsureEmptyResultHandler(errorHandler, handler), errorHandler, headerHandler);
        }
        /// <summary>
        /// Executes the "PUT" verb against the resource located at <paramref name="uri"/>. The request body is expected to be formatted in XML.
        /// </summary>
        /// <typeparam name="BODY">A type of object to be sent to the server.</typeparam>
        /// <param name="uri">The resource to be accessed.</param>
        /// <param name="body">The body of the request.</param>
        /// <param name="handler">The final handler.</param>
        /// <param name="errorHandler">The error handler.</param>
        /// <param name="headerHandler">Custom header handler.</param>
        public static void PutXml<BODY>(this Uri uri, BODY body, Action handler, Action<Exception> errorHandler, Action<HttpWebRequest> headerHandler = null)
        {
            HttpHelper.PutXml(uri, body.SerializeXml(), EnsureEmptyResultHandler(errorHandler, handler), errorHandler, headerHandler);
        }
        /// <summary>
        /// Executes the "PUT" verb against the resource located at <paramref name="uri"/>. The request body is expected to be formatted in JSON.
        /// </summary>
        /// <typeparam name="BODY">A type of object to be sent to the server.</typeparam>
        /// <param name="uri">The resource to be accessed.</param>
        /// <param name="body">The body of the request.</param>
        /// <param name="handler">The final handler.</param>
        /// <param name="errorHandler">The error handler.</param>
        /// <param name="headerHandler">Custom header handler.</param>
        public static void PutJson<BODY>(this Uri uri, BODY body, Action handler, Action<Exception> errorHandler, Action<HttpWebRequest> headerHandler = null)
        {
            HttpHelper.PutJson(uri, body.SerializeJson(), EnsureEmptyResultHandler(errorHandler, handler), errorHandler, headerHandler);
        }

        /// <summary>
        /// Executes the "PUT" verb against the resource located at <paramref name="uri"/>.
        /// </summary>
        /// <typeparam name="RESULT">The type of the result parameter.</typeparam>
        /// <param name="uri">The resource to be accessed.</param>
        /// <param name="handler">The handler receiving the resultant object.</param>
        /// <param name="errorHandler">The error handler.</param>
        /// <param name="deserializer">A method capable of deserializing the resultant object.</param>
        /// <param name="headerHandler">Custom header handler.</param>
        public static void Put<RESULT>(this Uri uri, Action<RESULT> handler, Action<Exception> errorHandler,
            Func<string, RESULT> deserializer, Action<HttpWebRequest> headerHandler = null)
        {
            uri.Put(res => handler(deserializer(res)), errorHandler, headerHandler);
        }
        /// <summary>
        /// Executes the "PUT" verb against the resource located at <paramref name="uri"/>. The request body is expected to be formatted in XML.
        /// </summary>
        /// <typeparam name="RESULT">The type of the result parameter.</typeparam>
        /// <param name="uri">The resource to be accessed.</param>
        /// <param name="handler">The handler receiving the resultant object.</param>
        /// <param name="errorHandler">The error handler.</param>
        /// <param name="headerHandler">Custom header handler.</param>
        public static void PutXml<RESULT>(this Uri uri, Action<RESULT> handler, Action<Exception> errorHandler, Action<HttpWebRequest> headerHandler = null)
        {
            uri.PutXml(null, res => handler(res.DeserializeXml<RESULT>()), errorHandler, headerHandler);
        }
        /// <summary>
        /// Executes the "PUT" verb against the resource located at <paramref name="uri"/>. The request body is expected to be formatted in JSON.
        /// </summary>
        /// <typeparam name="RESULT">The type of the result parameter.</typeparam>
        /// <param name="uri">The resource to be accessed.</param>
        /// <param name="handler">The handler receiving the resultant object.</param>
        /// <param name="errorHandler">The error handler.</param>
        /// <param name="headerHandler">Custom header handler.</param>
        public static void PutJson<RESULT>(this Uri uri, Action<RESULT> handler, Action<Exception> errorHandler, Action<HttpWebRequest> headerHandler = null)
        {
            uri.PutJson(null, res => handler(res.DeserializeJson<RESULT>()), errorHandler, headerHandler);
        }

        /// <summary>
        /// Executes the "PUT" verb against the resource located at <paramref name="uri"/>.
        /// </summary>
        /// <typeparam name="BODY">A type of object to be sent to the server.</typeparam>
        /// <typeparam name="RESULT">The type of the result parameter.</typeparam>
        /// <param name="uri">The resource to be accessed.</param>
        /// <param name="body">The body of the request.</param>
        /// <param name="handler">The handler receiving the resultant object.</param>
        /// <param name="errorHandler">The error handler.</param>
        /// <param name="serializer">A method that is capable of serializing <paramref name="body"/>.</param>
        /// <param name="deserializer">A method capable of deserializing the resultant object.</param>
        /// <param name="headerHandler">Custom header handler.</param>
        public static void Put<BODY, RESULT>(this Uri uri, BODY body, Action<RESULT> handler, Action<Exception> errorHandler,
            Func<BODY, string> serializer, Func<string, RESULT> deserializer, Action<HttpWebRequest> headerHandler = null)
        {
            uri.Put(serializer(body), res => handler(deserializer(res)), errorHandler, headerHandler);
        }
        /// <summary>
        /// Executes the "PUT" verb against the resource located at <paramref name="uri"/>. The request body is expected to be formatted in XML.
        /// </summary>
        /// <typeparam name="BODY">A type of object to be sent to the server.</typeparam>
        /// <typeparam name="RESULT">The type of the result parameter.</typeparam>
        /// <param name="uri">The resource to be accessed.</param>
        /// <param name="body">The body of the request.</param>
        /// <param name="handler">The handler receiving the resultant object.</param>
        /// <param name="errorHandler">The error handler.</param>
        /// <param name="headerHandler">Custom header handler.</param>
        public static void PutXml<BODY, RESULT>(this Uri uri, BODY body, Action<RESULT> handler, Action<Exception> errorHandler, Action<HttpWebRequest> headerHandler = null)
        {
            uri.PutXml(body.SerializeXml(), res => handler(res.DeserializeXml<RESULT>()), errorHandler, headerHandler);
        }
        /// <summary>
        /// Executes the "PUT" verb against the resource located at <paramref name="uri"/>. The request body is expected to be formatted in JSON.
        /// </summary>
        /// <typeparam name="BODY">A type of object to be sent to the server.</typeparam>
        /// <typeparam name="RESULT">The type of the result parameter.</typeparam>
        /// <param name="uri">The resource to be accessed.</param>
        /// <param name="body">The body of the request.</param>
        /// <param name="handler">The handler receiving the resultant object.</param>
        /// <param name="errorHandler">The error handler.</param>
        /// <param name="headerHandler">Custom header handler.</param>
        public static void PutJson<BODY, RESULT>(this Uri uri, BODY body, Action<RESULT> handler, Action<Exception> errorHandler, Action<HttpWebRequest> headerHandler = null)
        {
            uri.PutJson(body.SerializeJson(), res => handler(res.DeserializeJson<RESULT>()), errorHandler, headerHandler);
        }
    }
}